package com.fks.fks_service.controller;

import com.fks.fks_service.base.JsonResult;
import com.fks.fks_service.base.ResultCode;
import com.fks.fks_service.mapper.*;
import com.fks.fks_service.pojo.Admin;
import com.fks.fks_service.pojo.CategoryDB;
import com.fks.fks_service.pojo.ShopDB;
import com.fks.fks_service.pojo.ShopReleaseRecord;
import com.fks.fks_service.utils.DateUtils;
import com.fks.fks_service.utils.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Transactional//开启事务回滚
@Api(tags = "商品模块")
public class ShopController {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    ShopReleaseRecordMapper shopReleaseRecordMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    SeeMapper seeMapper;


    /**
     * 获取商城商品列表
     * @param request page当前页 pageSize每页数量
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "param"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "param"),
    })
    @ApiOperation(value = "获取商城商品列表", notes = "获取商城商品列表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = ShopDB.class)
    })
    @PostMapping("/api/getShopList")
    public Map<String, Object> getShopList(HttpServletRequest request){
        //请求参数
        if (request.getParameter("page")==null || request.getParameter("pageSize")==null) {
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        if (page == 0) {
            page = 1;
        }
        if (pageSize == 0) {
            pageSize = 10;
        }
        List<ShopDB> shopList = shopMapper.getShopList((page - 1) * pageSize, pageSize);
        int total = shopMapper.getShopListNumber();
        Map<String, Object> map = new HashMap<>();
        map.put("data",shopList);
        map.put("total",total);
        map.put("code",200);
        map.put("msg","获取商城商品列表成功");
        return map;
    }

    /**
     * 获取商城商品详情
     * @param request id商品id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = false,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = false,paramType = "header"),
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "param"),
    })
    @ApiOperation(value = "用户端获取商城商品详情", notes = "用户端获取商城商品详情（会增加浏览次数）")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = ShopDB.class)
    })
    @PostMapping("/api/getShop")
    public Map<String, Object> getShop(HttpServletRequest request) {
        //请求参数
        if (request.getParameter("id")==null) {
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        ShopDB shopDB = shopMapper.getShop(Integer.parseInt(request.getParameter("id")));
        if (shopDB==null){
            return JsonResult.error(2000,"商品不存在");
        }
        //增加浏览次数
        shopMapper.addSeeNum(shopDB.getId(),shopDB.getSeeNum()+1);
        try{
            String token = request.getHeader("token");
            //获取用户id
            Claims tokenClaims = JwtTokenUtil.getClaims(token);
            int userId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
            //增加浏览记录
            seeMapper.addSee(userId,shopDB.getId(),DateUtils.getNowTime());
        }catch (Exception e){
        }
        return JsonResult.success("获取商城商品详情成功").data(shopDB);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "param"),
    })
    @ApiOperation(value = "管理端获取商城商品详情", notes = "管理端获取商城商品详情（不增加浏览次数）")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = ShopDB.class)
    })
    @PostMapping("/api/getAdminShop")
    public Map<String, Object> getAdminShop(HttpServletRequest request) {
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //请求参数
        if (request.getParameter("id")==null||"".equals(request.getParameter("id"))) {
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        ShopDB shopDB = shopMapper.getShop(Integer.parseInt(request.getParameter("id")));
        return JsonResult.success("获取商城商品详情成功").data(shopDB);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "id",value = "商品id",required = false,paramType = "param"),
            @ApiImplicitParam(name = "title",value = "商品标题",required = false,paramType = "param"),
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = false,paramType = "param"),
            @ApiImplicitParam(name = "page",value = "当前页数",required = false,paramType = "param"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = false,paramType = "param"),
            @ApiImplicitParam(name = "status",value = "商品状态,显示全部为空，1销售，2已下架",required = false,paramType = "param"),
            @ApiImplicitParam(name = "isDelete",value = "是否删除",required = false,paramType = "param"),
    })
    @ApiOperation(value = "管理员按需求获取商品列表", notes = "管理员按需求获取商品列表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/getMerchantUserShopList")
    public Map<String, Object> getMerchantUserShopList(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"merchantUser".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        Map<String, Object> map = new HashMap<>();
        //校验参数
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String categoryName = request.getParameter("categoryName");
        String page = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        String status = request.getParameter("status");
        String isDelete = request.getParameter("isDelete");
        if (token==null){
            map.put("code",2000);
            map.put("msg","获取商品失败");
            return map;
        }
        if (id==null||id==""){
            id = "0";
        }
        if (title==null){
            title = "";
        }
        if (categoryName==null){
            categoryName = "";
        }
        if (page==null||page==""){
            page = "0";
        }
        if (pageSize==null||pageSize==""){
            pageSize = "10";
        }
        if (status==null||status==""){
            status = "0";
        }
        if (isDelete==null){
            isDelete="";
        }

        int total = shopMapper.getAdminShopListNumber(id,title,categoryName,status,isDelete);

        List<ShopDB> list = shopMapper.getAdminShopList(id,title,categoryName,page,pageSize,status,isDelete);
        map.put("code",200);
        map.put("data",list);
        map.put("total",total);
        map.put("msg","获取商品成功");
        return map;
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "numbers",value = "需要恢复的分类编号",required = true,paramType = "param"),
    })
    @ApiOperation(value = "批量恢复商品", notes = "批量恢复商品")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/restoreShop")
    public JsonResult restoreShop(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"admin".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        //获取管理员对应商家信息
        Admin admin = adminMapper.getById(adminId);

        String numbers = request.getParameter("numbers");
        if (numbers==null||numbers==""){
            return JsonResult.error(2000,"参数为空");
        }
        String[] split2 = numbers.split("");
        numbers="";
        for (int i = 1; i < split2.length-1; i++) {
            numbers=numbers+split2[i];
        }
        String[] splits = numbers.split(",");
        String split1 = "";
        if (splits.length>=1){
            for (String split : splits) {
                shopMapper.restoreShop(Integer.parseInt(split));

                shopMapper.updateShopTime(DateUtils.getNowTime(),Integer.parseInt(split));

                //记录商品修改信息
                ShopReleaseRecord shopReleaseRecord = new ShopReleaseRecord();
                ShopDB shop = shopMapper.getShop(Integer.parseInt(split));
                shopReleaseRecord.setTitle(shop.getTitle());
                shopReleaseRecord.setShopId(Integer.parseInt(split));
                shopReleaseRecord.setUpdateStatus("已上架");
                shopReleaseRecord.setEditingType("4");
                shopReleaseRecord.setEditAccount(String.valueOf(admin.getId()));
                shopReleaseRecord.setUpdateTime(DateUtils.getNowTime());
                shopReleaseRecord.setBannerImage(shop.getBannerImages());
                shopReleaseRecord.setCategoryName1(shop.getCategoryName1());
                shopReleaseRecord.setCategoryName2(shop.getCategoryName2());
                shopReleaseRecord.setCategoryName3(shop.getCategoryName3());
                shopReleaseRecordMapper.addShopReleaseRecord(shopReleaseRecord);
            }
        }else {
            return JsonResult.error(2000,"编号为空");
        }

        return JsonResult.success("批量恢复商品成功");
    }

    /**
     * 删除商品
     * @param request token  id商品id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "numbers",value = "需要删除的分类编号",required = true,paramType = "param"),
    })
    @ApiOperation(value = "批量删除商品", notes = "批量删除商品")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/deleteShop")
    public JsonResult deleteShop(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"admin".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        //获取管理员对应商家信息
        Admin admin = adminMapper.getById(adminId);
        String numbers = request.getParameter("numbers");
        if (numbers==null||numbers==""){
            return JsonResult.error(2000,"参数为空");
        }
        String[] split2 = numbers.split("");
        numbers="";
        for (int i = 1; i < split2.length-1; i++) {
            numbers=numbers+split2[i];
        }
        String[] splits = numbers.split(",");
        String split1 = "";
        if (splits.length>=1){
            for (String split : splits) {
                shopMapper.deleteShop(Integer.parseInt(split));

                shopMapper.updateShopTime(DateUtils.getNowTime(),Integer.parseInt(split));

                //记录商品修改信息
                ShopReleaseRecord shopReleaseRecord = new ShopReleaseRecord();
                ShopDB shop = shopMapper.getShop(Integer.parseInt(split));
                if (shop==null){
                    return JsonResult.error(2000,"商品不存在或已被删除");
                }
                shopReleaseRecord.setTitle(shop.getTitle());
                shopReleaseRecord.setShopId(Integer.parseInt(split));
                shopReleaseRecord.setUpdateStatus("已删除");
                shopReleaseRecord.setEditingType("3");
                shopReleaseRecord.setEditAccount(String.valueOf(admin.getId()));
                shopReleaseRecord.setUpdateTime(DateUtils.getNowTime());
                shopReleaseRecord.setBannerImage(shop.getBannerImages());
                shopReleaseRecord.setCategoryName1(shop.getCategoryName1());
                shopReleaseRecord.setCategoryName2(shop.getCategoryName2());
                shopReleaseRecord.setCategoryName3(shop.getCategoryName3());
                shopReleaseRecordMapper.addShopReleaseRecord(shopReleaseRecord);
            }
        }else {
            return JsonResult.error(2000,"编号为空");
        }

        return JsonResult.success("删除成功");
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "title",value = "商品标题",required = true,paramType = "param"),
            @ApiImplicitParam(name = "introduction",value = "商品详情描述",required = true,paramType = "param"),
            @ApiImplicitParam(name = "categoryId",value = "分类id",required = true,paramType = "param"),
            @ApiImplicitParam(name = "detailImages",value = "商品展示图片",required = true,paramType = "param"),
            @ApiImplicitParam(name = "bannerImages",value = "商品轮播图",required = true,paramType = "param"),
            @ApiImplicitParam(name = "price",value = "商品价格",required = true,paramType = "param"),
            @ApiImplicitParam(name = "salesVolume",value = "销量",required = true,paramType = "param"),
            @ApiImplicitParam(name = "promisedDeliveryTime",value = "承诺发货时间",required = true,paramType = "param"),
            @ApiImplicitParam(name = "isFreeShipping",value = "是否包邮,0不包邮，1包邮",required = true,paramType = "param"),
            @ApiImplicitParam(name = "expressCompany",value = "快递公司",required = true,paramType = "param"),
            @ApiImplicitParam(name = "freight",value = "运费，如果包邮就填0",required = true,paramType = "param"),
    })
    @ApiOperation(value = "新增商城商品", notes = "新增商城商品")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/addShop")
    public JsonResult addShop(HttpServletRequest request){
        //检测权限

        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"admin".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        Admin admin = adminMapper.getById(adminId);

        String title = request.getParameter("title");//商品标题
        String introduction = request.getParameter("introduction");//商品详情描述
        String categoryId = request.getParameter("categoryId");//分类id
        String bannerImages = request.getParameter("bannerImages");//商品展示图片
        String detailImages = request.getParameter("detailImages");//商品轮播图
        String price = request.getParameter("price");//商品价格
        if (title==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少商品标题");
        }else if (detailImages==null||"[]".equals(detailImages)){
            return JsonResult.error(ResultCode.param_is_null,"缺少商品轮播图");
        }else if (introduction==null||"[]".equals(introduction)){
            return JsonResult.error(ResultCode.param_is_null,"缺少商品详情描述");
        }else if (price==null||"[]".equals(price)){
            return JsonResult.error(ResultCode.param_is_null,"缺少商品价格");
        }else if (categoryId==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少分类id");
        }else if (bannerImages==null||"[]".equals(bannerImages)){
            return JsonResult.error(ResultCode.param_is_null,"缺少商品展示图片");
        }else if (request.getParameter("salesVolume")==null||request.getParameter("salesVolume").equals("")){
            return JsonResult.error(ResultCode.param_is_null,"缺少设置的销量");
        }
        int salesVolume = 0;
        try{
            salesVolume = Integer.parseInt(request.getParameter("salesVolume"));//设置的销量
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(2000,"销量必须为整数");
        }
        //获取分类信息
        CategoryDB category = categoryMapper.getCategoryById(Integer.parseInt(categoryId));
        if (category.getCategoryName2()==null){
            category.setCategoryName2("");
        }
        if (category.getCategoryName3()==null){
            category.setCategoryName3("");
        }

        //设置新商品
        ShopDB shopDB = new ShopDB();
        shopDB.setTitle(title);
        shopDB.setIntroduction(introduction);
        shopDB.setStatus(1);//状态--0、不销售  1、销售 2、已售罄
        shopDB.setSeeNum(0);//商品浏览数量
        shopDB.setCreateTime(DateUtils.getNowTime());
        shopDB.setIsDelete(0);//是否删除--0、不删除  1、删除
        shopDB.setCreateAdminId(adminId);
        shopDB.setCategoryId(Integer.parseInt(categoryId));
        shopDB.setDetailImages(detailImages);
        shopDB.setBannerImages(bannerImages);
        shopDB.setPrice(price);
        shopDB.setSalesVolume(salesVolume);
        shopDB.setCategoryName1(category.getCategoryName1());
        shopDB.setCategoryName2(category.getCategoryName2());
        shopDB.setCategoryName3(category.getCategoryName3());
        shopMapper.addShop(shopDB);

        int shopId = shopMapper.getShopByShop(shopDB);

        //记录商品修改信息
        ShopReleaseRecord shopReleaseRecord = new ShopReleaseRecord();
        shopReleaseRecord.setTitle(shopDB.getTitle());
        shopReleaseRecord.setShopId(shopId);
        shopReleaseRecord.setUpdateStatus("已上架");
        shopReleaseRecord.setEditingType("1");
        shopReleaseRecord.setMerchantUserId(adminId);
        shopReleaseRecord.setUpdateTime(DateUtils.getNowTime());
        shopReleaseRecordMapper.addShopReleaseRecord(shopReleaseRecord);

        return JsonResult.success("新增商城商品成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "title",value = "商品标题",required = true,paramType = "param"),
            @ApiImplicitParam(name = "introduction",value = "商品详情描述",required = true,paramType = "param"),
            @ApiImplicitParam(name = "status",value = "状态--1，销售，2，已下架",required = true,paramType = "param"),
            @ApiImplicitParam(name = "isDelete",value = "是否删除--0、不删除  1、删除",required = true,paramType = "param"),
            @ApiImplicitParam(name = "categoryId",value = "分类id",required = true,paramType = "param"),
            @ApiImplicitParam(name = "detailImages",value = "商品展示图片",required = true,paramType = "param"),
            @ApiImplicitParam(name = "bannerImages",value = "商品轮播图",required = true,paramType = "param"),
            @ApiImplicitParam(name = "price",value = "商品价格",required = true,paramType = "param"),
            @ApiImplicitParam(name = "salesVolume",value = "销量",required = true,paramType = "param"),
    })
    @ApiOperation(value = "修改商品信息", notes = "修改商品信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/updateShop")
    public JsonResult updateShop(HttpServletRequest request){
        //校验参数
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"admin".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        //获取管理员对应商家id
        Admin admin = adminMapper.getById(adminId);
        String merchantId = String.valueOf(admin.getId());

        String id = request.getParameter("id");
        String title = request.getParameter("title");//商品标题
        String introduction = request.getParameter("introduction");//商品标题
        String status = request.getParameter("status");//状态--1，销售，2，已下架
        String isDelete = request.getParameter("isDelete");//是否删除--0、不删除  1、删除
        String categoryId = request.getParameter("categoryId");//分类id
        String bannerImages = request.getParameter("bannerImages");//商品展示图片
        String detailImages = request.getParameter("detailImages");//商品轮播图
        String price = request.getParameter("price");//商品价格
        if (title==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (introduction==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (categoryId==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (price==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (isDelete==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (status==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (id==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (bannerImages==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (detailImages==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }else if (request.getParameter("salesVolume")==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        int salesVolume = Integer.parseInt(request.getParameter("salesVolume"));//设置的销量
//        //判断管理员和商品是否归属同一商家
//        ShopDB shop = shopService.get(Integer.parseInt(id));
//        if (shop.getBusinessId()!=businessId){
//            return JsonResult.error(ResultCode.param_is_null,"不能修改其他商家商品");
//        }

        //设置新商品信息
        ShopDB shopDB = new ShopDB();
        shopDB.setId(Integer.parseInt(id));
        shopDB.setTitle(title);
        shopDB.setIntroduction(introduction);
        shopDB.setStatus(Integer.parseInt(status));
        shopDB.setUpdateTime(DateUtils.getNowTime());
        shopDB.setIsDelete(Integer.parseInt(isDelete));
        shopDB.setCategoryId(Integer.parseInt(categoryId));
        shopDB.setDetailImages(detailImages);
        shopDB.setBannerImages(bannerImages);
        shopDB.setPrice(price);
        shopDB.setSalesVolume(salesVolume);
        shopMapper.updateShop(shopDB);

        //记录商品修改信息
        ShopReleaseRecord shopReleaseRecord = new ShopReleaseRecord();
        ShopDB shop = shopMapper.getShop(Integer.parseInt(id));
        shopReleaseRecord.setTitle(shop.getTitle());
        shopReleaseRecord.setShopId(Integer.parseInt(id));
        shopReleaseRecord.setUpdateStatus("已上架");
        shopReleaseRecord.setEditingType("5");
        shopReleaseRecord.setEditAccount(String.valueOf(admin.getId()));
        shopReleaseRecord.setMerchantUserId(adminId);
        shopReleaseRecord.setUpdateTime(DateUtils.getNowTime());
        shopReleaseRecord.setBannerImage(shop.getBannerImages());
        shopReleaseRecord.setCategoryName1(shop.getCategoryName1());
        shopReleaseRecord.setCategoryName2(shop.getCategoryName2());
        shopReleaseRecord.setCategoryName3(shop.getCategoryName3());
        shopReleaseRecordMapper.addShopReleaseRecord(shopReleaseRecord);

//        //设置商品展示轮播图
//        int bannerImages = Integer.parseInt(bannerImagesNumber);
//        for (int i = 0; i < bannerImages; i++) {
//            String bannerImage = request.getParameter("bannerImages" + i);
//            if (bannerImage==null){
//                return JsonResult.error(ResultCode.param_is_null,"缺少参数");
//            }
//            BannerImage image =bannerImagesMapper.getBannerImage(bannerImage,Integer.parseInt(id));
//            if (image==null){
//                bannerImagesMapper.addBannerImages(bannerImage,userId,DateUtils.getNowTime(),"1",Integer.parseInt(id));
//            }else {
//
//            }
//        }
        return JsonResult.success("修改成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "param"),
            @ApiImplicitParam(name = "status",value = "商品状态--1，销售，2，已下架",required = true,paramType = "param"),
    })
    @ApiOperation(value = "批量上下架商品", notes = "批量上下架商品")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/UpAndDownShelves")
    public JsonResult UpAndDownShelves(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(2000,"token为空");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"admin".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        //获取管理员对应商家id
        Admin admin = adminMapper.getById(adminId);
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        if (id==null){
            return JsonResult.error(2000,"id为空");
        }else if (status==null){
            return JsonResult.error(2000,"status为空");
        }
        String[] split2 = id.split("");
        id="";
        for (int i = 1; i < split2.length-1; i++) {
            id=id+split2[i];
        }
        String[] splits = id.split(",");
        String split1 = "";
        if (splits.length>=1){
            for (String split : splits) {
                shopMapper.updateStatus(Integer.valueOf(split),Integer.valueOf(status));

                shopMapper.updateShopTime(DateUtils.getNowTime(),Integer.parseInt(split));

                //记录商品修改信息
                ShopReleaseRecord shopReleaseRecord = new ShopReleaseRecord();
                ShopDB shop = shopMapper.getShop(Integer.parseInt(split));
                if (shop==null){
                    return JsonResult.error(2000,"商品不存在");
                }
                shopReleaseRecord.setTitle(shop.getTitle());
                shopReleaseRecord.setShopId(Integer.parseInt(split));
                shopReleaseRecord.setUpdateStatus("发布成功");
                if ("1".equals(status)){
                    shopReleaseRecord.setEditingType("1");
                }else {
                    shopReleaseRecord.setEditingType("2");
                }
                shopReleaseRecord.setEditAccount(String.valueOf(admin.getId()));
                shopReleaseRecord.setUpdateTime(DateUtils.getNowTime());
                shopReleaseRecord.setBannerImage(shop.getBannerImages());
                shopReleaseRecord.setCategoryName1(shop.getCategoryName1());
                shopReleaseRecord.setCategoryName2(shop.getCategoryName2());
                shopReleaseRecord.setCategoryName3(shop.getCategoryName3());
                shopReleaseRecordMapper.addShopReleaseRecord(shopReleaseRecord);
            }
        }else {
            return JsonResult.error(2000,"编号为空");
        }



        return JsonResult.success("改变状态成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "param"),
            @ApiImplicitParam(name = "price",value = "商品价格",required = true,paramType = "param"),
    })
    @ApiOperation(value = "修改商品价格", notes = "修改商品价格")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/UpAndDownPrice")
    public JsonResult UpAndDownPrice(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(2000,"token为空");
        }
        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int adminId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"admin".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }
        //获取管理员对应商家id
        Admin admin = adminMapper.getById(adminId);
        String id = request.getParameter("id");
        String price = request.getParameter("price");
        if (id==null){
            return JsonResult.error(2000,"id为空");
        }else if (price==null){
            return JsonResult.error(2000,"总价为空");
        }
        shopMapper.updatePrice(Integer.valueOf(id),price);

        shopMapper.updateShopTime(DateUtils.getNowTime(),Integer.parseInt(id));

        //记录商品修改信息
        ShopReleaseRecord shopReleaseRecord = new ShopReleaseRecord();
        ShopDB shop = shopMapper.getShop(Integer.parseInt(id));
        shopReleaseRecord.setTitle(shop.getTitle());
        shopReleaseRecord.setShopId(Integer.parseInt(id));
        shopReleaseRecord.setUpdateStatus("已上架");
        shopReleaseRecord.setEditingType("6");
        shopReleaseRecord.setEditAccount(String.valueOf(adminId));
        shopReleaseRecord.setUpdateTime(DateUtils.getNowTime());
        shopReleaseRecord.setBannerImage(shop.getBannerImages());
        shopReleaseRecord.setCategoryName1(shop.getCategoryName1());
        shopReleaseRecord.setCategoryName2(shop.getCategoryName2());
        shopReleaseRecord.setCategoryName3(shop.getCategoryName3());
        shopReleaseRecordMapper.addShopReleaseRecord(shopReleaseRecord);

        return JsonResult.success("修改商品价格成功");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "jwtRefreshToken",value = "是否重置认证令牌",required = true,paramType = "header"),
            @ApiImplicitParam(name = "page",value = "当前页数",required = false,paramType = "param"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = false,paramType = "param"),
            @ApiImplicitParam(name = "shopId",value = "商品id",required = false,paramType = "param"),
            @ApiImplicitParam(name = "title",value = "商品名称",required = false,paramType = "param"),
            @ApiImplicitParam(name = "updateStatus",value = "商品状态",required = false,paramType = "param"),
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = false,paramType = "param"),
            @ApiImplicitParam(name = "editingType",value = "编辑类型,1上架，2下架，3删除，4恢复，5修改",required = false,paramType = "param"),
            @ApiImplicitParam(name = "newOldDegree",value = "成色，全新：100，九八新：98，九五新：95",required = false,paramType = "param"),
    })
    @ApiOperation(value = "获取商品修改记录列表", notes = "获取商品修改记录列表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = String.class)
    })
    @PostMapping("/api/getShopReleaseRecord")
    public Map<String, Object> getShopReleaseRecord(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();

        String token = request.getHeader("token");
        if (token==null){
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }

        //获取用户id和账号类型
        Claims tokenClaims = JwtTokenUtil.getClaims(token);
        int merchantUserId = Integer.parseInt(String.valueOf(tokenClaims.get("userId")));
        //权限判断-商家
        String accountType = String.valueOf(tokenClaims.get("accountType"));
        if (!"merchantUser".equals(accountType)){
            return JsonResult.error(ResultCode.param_is_null,"权限不足");
        }

        //校验参数
        String id = request.getParameter("shopId");
        String title = request.getParameter("title");
        String updateStatus = request.getParameter("updateStatus");
        String categoryName = request.getParameter("categoryName");
        String editingType = request.getParameter("editingType");
        String newOldDegree = request.getParameter("newOldDegree");
        String page = request.getParameter("page");
        String pageSize = request.getParameter("pageSize");
        if (id==null){
            id = "";
        }
        if (title==null){
            title = "";
        }
        if (updateStatus==null){
            updateStatus = "";
        }
        if (newOldDegree==null){
            newOldDegree =  "";
        }
        if (categoryName==null){
            categoryName = "";
        }
        if (page==null||page==""){
            page = "0";
        }
        if (pageSize==null||pageSize==""){
            pageSize = "10";
        }
        if (editingType==null){
            editingType = "";
        }

        int total = shopReleaseRecordMapper.getShopReleaseRecordNumber(id,title,updateStatus
                ,newOldDegree,categoryName,editingType,merchantUserId);

        List<ShopReleaseRecord> list = shopReleaseRecordMapper.getShopReleaseRecord(id,title,updateStatus
                ,newOldDegree,categoryName,page,pageSize,editingType,merchantUserId);

        map.put("code",200);
        map.put("msg","获取商品修改记录列表成功");
        map.put("data",list);
        map.put("total",total);
        return map;
    }



}
